/**
 * Solution.java
 * 05/12/2012
 * @author Diego Garcia
 * diego.mendiburu@gmail.com
 * 
 * Copyright (C) 2012  Diego Garcia
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package mx.truenite.Triplets;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.TreeSet;

/**
 * 
 */
public class Solution {
	public static void main(String[] args) throws FileNotFoundException {
		File file = new File("input01.txt");
		Scanner sc = new Scanner(file);
		//sc = new Scanner(System.in);
		if (sc.hasNext()) {
			// reading input
			/*int N = sc.nextInt();
			int[] array = new int[N];
			array[0]=sc.nextInt();
			for(int i = 1; i < N; i++){
				int add = sc.nextInt();
				if(array[i-1]!=add)
					array[i]=add;
				else{
					i--;
					N--;
				}
			}
			//System.out.println(printArray(array)+" size: "+array.length);
			int i = 0;
			int j = i+1;
			int h = j+1;
			int limit = N;
			TreeSet<Triplet> set = new TreeSet<Triplet>();
			TreeSet<Integer> haveBeenI = new TreeSet<Integer>();
			TreeSet<Integer> haveBeenJ = new TreeSet<Integer>();
			for(i=0; i <limit-2; i++){
				if(!haveBeenI.contains(array[i])){
					int ini = array[i];
					haveBeenI.add(ini);
					//array[i]=-1;
					for(j=i+1; j<limit-1;j++){
						if(array[j]>ini){
							int inj = array[j];
							haveBeenJ.add(inj);
							//array[j]=-1;
							for(h=j+1; h<limit;h++){
								if(array[h]>inj){
									int inh = array[h];
									//array[h]=-1;
									//System.out.println("i: "+ini+" j: "+inj+" h: "+inh);
									set.add(new Triplet(ini,inj,inh));
									
								}
							}
						}
					}
				}
			}
			/*System.out.println(printArray(array));
			System.out.println(set);
			System.out.println(set.size());
			System.out.println("2nd way");*/
			sc = new Scanner(file);
			int N=sc.nextInt();
			TreeSet<Triplet> set2 = new TreeSet<Triplet>();
			ArrayList<Triplet> duets = new ArrayList<Triplet>();
			int[] array = new int[N];
			array[0]=sc.nextInt();
			int i,j;
			Triplet first = new Triplet(array[0]);
			duets.add(first);
			for(i = 1; i < N; i++){
				int add = sc.nextInt();
				if(array[i-1]!=add){
					array[i]=add;
					//System.out.println(duets);
					Triplet clone;
					int size = duets.size();
					for(j = 0; j<size;j++){
						clone = duets.get(j).clone();
						if(clone.add(add)){
							set2.add(clone);
						}else{
							//System.out.println("clone: "+clone);
							duets.add(clone);
						}
					}
					//duets.addAll(clones);
					Triplet newOne = new Triplet(add);
					duets.add(newOne);
				}
				else{
					i--;
					N--;
				}
			}
			/*System.out.println();
			System.out.println(set);
			System.out.println();
			System.out.println(set2);*/
			System.out.println(set2.size());
		}
	}
	public static String printArray(int[] array){
		String str = "{";
		for(int i = 0; i<array.length;i++)
			str+=" "+array[i];
		str+="}";
		return str;
	}
	public static boolean contains(int[] array, int n){
		for(int i = 0; i<array.length; i++){
			if(array[i]==n)
				return true;
			if(array[i]==0)
				return false;
		}
		return false;
	}
}
class Triplet implements Comparable<Triplet>{
	int a;
	int b;
	int c;
	public Triplet(int a, int b, int c){
		this.a = a;
		this.b = b;
		this.c = c;
	}
	public Triplet(int a){
		this.a = a;
	}
	@Override
	public int compareTo(Triplet o) {
		if(this.a < o.a)
			return -1;
		if(this.a > o.a)
			return 1;
		if(this.b < o.b)
			return -1;
		if(this.b > o.b)
			return 1;
		if(this.c < o.c)
			return -1;
		if(this.c > o.c)
			return 1;
		return 0;
	}
	public String toString(){
		return "["+a+" "+b+" "+c+"]";
	}
	public Triplet clone(){
		return new Triplet(a,b,c);
	}
	public boolean add(int n){
		if(b==0 && n>a){
			b=n;
			return false;
		}
		if(c==0 && b!=0 && n>b){
			c=n;
			return true;
		}
		return false;
	}
}
